from pydantic import BaseModel, Field, field_validator
from datetime import datetime
from typing import Optional
import re

# 用户相关的Schema
class UserBase(BaseModel):
    name: str = Field(..., min_length=1, max_length=50)
    avator: Optional[str] = None

class UserCreate(UserBase):
    pwd: str = Field(..., min_length=6)
    
    @field_validator('name')
    @classmethod
    def validate_name(cls, v):
        # 验证用户名只能包含英文和中文
        if not re.match(r'^[a-zA-Z\u4e00-\u9fa5]+$', v):
            raise ValueError('用户名只能包含英文和中文字符')
        return v

class UserUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=50)
    avator: Optional[str] = None

# 之前的UserDTO定义已替换为完整的UserDTO类

class UserDTO(BaseModel):
    id: int
    name: str
    avator: Optional[str] = None
    create_at: datetime
    update_at: datetime

    class Config:
        from_attributes = True
